package com.appheader.webfragmentviewpagertest.views;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by zhep on 2016/9/26.
 */
public class MyHorizontalViewPager extends ViewPager {

    private float oldx;
    private float oldy;
    private float xDistance, yDistance, xLast, yLast;

    public MyHorizontalViewPager(Context context) {
        super(context);
    }

    public MyHorizontalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //拦截方法,保证水平ViewPager能够获取事件
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                if(xDistance > yDistance){
                    return true;
                }
        }
        return super.onInterceptTouchEvent(ev);

    }

    //    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//
//        switch (ev.getAction()){
//            case MotionEvent.ACTION_DOWN:
//                oldx = ev.getRawX();
//                oldy = ev.getRawY();
//                break;
//            case MotionEvent.ACTION_UP:
//                break;
//            case MotionEvent.ACTION_MOVE:
//                float distenceX = Math.abs(oldx - ev.getRawX());
//                float distenceY = Math.abs(oldy - ev.getRawY());
//                //限制必须得划过屏幕的1/4才能算划过
//                //float x_limit = 20;
//                //float y_limit = 20;
//                if (distenceX > distenceY ) {
//                    //Toast.makeText(MainActivity.this, "左右滑动", Toast.LENGTH_SHORT).show();
//                    Log.i(">>", "左右滑动" + ">>>" + distenceX);
//                    //vp.getParent().requestDisallowInterceptTouchEvent(true);
//                } else if (distenceY > distenceX ) {
//                    //Toast.makeText(MainActivity.this, "上下滑动", Toast.LENGTH_SHORT).show();
//                    Log.i(">>", "上下滑动" + ">>>" + distenceY);
//
//                }
//
//                oldx = ev.getRawX();
//                oldy = ev.getRawY();
//                getParent().requestDisallowInterceptTouchEvent(false);
//                break;
//        }
//        return true;
//    }
}
